#include "simple_app.h"
#include "include/wrapper/cef_helpers.h"
#include "simple_client.h"

SimpleApp::SimpleApp()
{
    LOG(INFO) << "CCefSimpleApp()";
}

SimpleApp::~SimpleApp()
{
    LOG(INFO) << "~CCefSimpleApp()";
}

void SimpleApp::OnContextInitialized()
{
    CEF_REQUIRE_UI_THREAD();
    LOG(INFO) << "OnContextInitialized()";

    SimpleClient::CreateMainWindow();
}

void SimpleApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
                                 CefRefPtr<CefFrame> frame,
                                 CefRefPtr<CefV8Context> context)
{
    CEF_REQUIRE_RENDERER_THREAD();
    ALLOW_UNUSED_LOCAL(browser);
    ALLOW_UNUSED_LOCAL(frame);
    LOG(INFO) << "OnContextCreated()" << ":browser" << browser->GetIdentifier();

    CefRefPtr<CefV8Value> object = context->GetGlobal(); //window object
    CefRefPtr<CefV8Value> test_str = CefV8Value::CreateString("Execute JavaScript works!");
    object->SetValue("teststr", test_str, V8_PROPERTY_ATTRIBUTE_NONE);

    //frame->ExecuteJavaScript("alert(window.teststr);", frame->GetURL(), 0);
}
